/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.domain.FileCapCCMGBDS;

/**
 * DON XIN CAP MOI, CAP LAI CHUNG CHI DINH GIA BDS
 * @author Ndtho
 *
 */
public class BmQT_CCDG_CMCL extends BaseReport{
	
	public InputStream toMSWord(InputStream docFile, FileCapCCMGBDS f) throws IOException {
	    
	    XWPFDocument docx = new XWPFDocument(docFile);
	    Iterator<XWPFTable> range=docx.getTablesIterator();

	    //Header
        XWPFTable tableHeader=range.next();
        
        //Skip image table
        range.next();
        
        //Form Detail
        XWPFTable tableContact=range.next();
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date today = new Date();
		String date =  dateFormat.format(today);
        
        /*==================== HEADER ====================*/
        XWPFTableRow header_row = tableHeader.getRow(1);
        XWPFRun header_column = header_row.getCell(0).getParagraphs().get(0).createRun();
        setTextFormat(header_column, getText("report.BmQT1201.header.date.first")+ " Ng\u00E0y "+date.substring(0, 2)+" th\u00E1ng "+date.substring(3, 5)+" n\u0103m "+date.substring(6, 10), FONT_SIZE_12, false, true, FONT_TIMES_NEW_ROMAN);

        /*==================== FORM DETAIL ====================*/
        XWPFTableRow row_0 = tableContact.getRow(0);
        setTextFormat(row_0.getCell(0).getParagraphs().get(0).createRun(), f.getFullname(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_1 = tableContact.getRow(1);
        setTextFormat(row_1.getCell(0).getParagraphs().get(0).createRun(), f.getDateOfBirth(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_2 = tableContact.getRow(2);
        setTextFormat(row_2.getCell(0).getParagraphs().get(0).createRun(), f.getPlaceOfBirth(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_3 = tableContact.getRow(3);
        setTextFormat(row_3.getCell(0).getParagraphs().get(0).createRun(), f.getNational(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_4 = tableContact.getRow(4);
        setTextFormat(row_4.getCell(0).getParagraphs().get(0).createRun(), f.getIdcard(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_5 = tableContact.getRow(5);
        setTextFormat(row_5.getCell(0).getParagraphs().get(0).createRun(), f.getIdcard_date(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_6 = tableContact.getRow(6);
        setTextFormat(row_6.getCell(0).getParagraphs().get(0).createRun(), f.getIdcard_place(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_8 = tableContact.getRow(7);
        setTextFormat(row_8.getCell(0).getParagraphs().get(0).createRun(), f.getAddress(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_9 = tableContact.getRow(8);
        setTextFormat(row_9.getCell(0).getParagraphs().get(0).createRun(), f.getEducation(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_10 = tableContact.getRow(9);
        setTextFormat(row_10.getCell(0).getParagraphs().get(0).createRun(), f.getCertificate(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_11 = tableContact.getRow(11);
        setTextFormat(row_11.getCell(0).getParagraphs().get(0).createRun(), f.getEdu_center_name(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_12 = tableContact.getRow(12);
        setTextFormat(row_12.getCell(0).getParagraphs().get(0).createRun(), f.getEdu_center_address(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        if(StringUtils.isNotEmpty( f.getLy_do_cap_lai())){
        	XWPFTableRow row_14 = tableContact.getRow(13);
        	setTextFormat(row_14.getCell(0).getParagraphs().get(0).createRun(), getText("filePublicAdministration.title.reason") + f.getLy_do_cap_lai(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        }

	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    
	    return new ByteArrayInputStream(baos.toByteArray());
	}
}
